package mil.af.amc.adpetracker.web.actions.Organization;

import mil.af.amc.adpetracker.web.actions.AbstractDispatchAction;
import mil.af.amc.adpetracker.service.OrganizationService;
import mil.af.amc.adpetracker.model.Organization;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class OrganizationDispatchAction extends AbstractDispatchAction {

    private OrganizationService organizationService;

    public void setOrganizationService(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    public ActionForward viewOrganizationList(ActionMapping actionMapping,
                                              ActionForm actionForm,
                                              HttpServletRequest httpServletRequest,
                                              HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewIndex");

    }

    public ActionForward viewAddOrganization(ActionMapping actionMapping,
                                              ActionForm actionForm,
                                              HttpServletRequest httpServletRequest,
                                              HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAddOrganization");

    }

    public ActionForward addOrganization(ActionMapping actionMapping,
                                              ActionForm actionForm,
                                              HttpServletRequest httpServletRequest,
                                              HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        ActionMessages errors = form.validate(actionMapping, httpServletRequest);
        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("viewAddOrganization");
        }

        Organization org = organizationService.add(form.getMap());

        return actionMapping.findForward("viewIndex");

    }
}
